/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Anna SAELEE
 */
public class ReadMessage extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn;
        Statement stmt;
        ResultSet rs;
        try {
            HttpSession session = request.getSession(true);
            String username = (String) session.getAttribute("username");
            session.setAttribute("username", username);

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dormitorymanagement", "root", "root");
            stmt = conn.createStatement();

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ReadMessage</title>");
            out.println("</head>");
            out.println("<body>");

            String typeMss = request.getParameter("typeMss");

            if (typeMss.equals("readOrder")) {
                int mid = Integer.parseInt(request.getParameter("id"));
                String sql = "SELECT date, s_username, r_number, message FROM service WHERE sid=" + mid;
                rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    out.println("DATE: " + rs.getString("date") + "<br/>");
                    out.println("Expéditeur: " + rs.getString("s_username") + "<br/>");
                    out.println("Chambre: " + rs.getString("r_number") + "<br/>");
                    out.println("Message: " + rs.getString("message") + "<br/>");
                    if (username.equals("admin")) {
                        out.println("<form method=\"post\" action=\"reponseService.jsp\" name=\"\">");
                        out.println("<input type=\"hidden\" name=\"mid\" value=\"" + mid + "\"/>");
                        out.println("<input type=\"submit\" value=\"Répondre à cette demande\"/>");
                        out.println("</form>");
                    } else if (!username.equals("admin")) {
                        out.println("<form method=\"post\" action=\"reponseService.jsp\" name=\"\">");
                        out.println("<input type=\"hidden\" name=\"mid\" value=\"" + mid + "\"/>");
                        out.println("<input type=\"submit\" value=\"Répondre à l'administrateur\"/>");
                        out.println("</form>");
                    }
                }
                String update = "UPDATE service SET status='o' WHERE sid=" + mid;
                int i = stmt.executeUpdate(update);
                if (i == 1) {
                    out.println("<br/><a href=\"listOrder.jsp\"> Retournez à la page de liste des services</a>");
                }
            } else if (typeMss.equals("readMss")) {
                int mid = Integer.parseInt(request.getParameter("id"));
                String sql = "SELECT subject, date, name, email, message FROM message WHERE mid=" + mid;
                rs = stmt.executeQuery(sql);
                while (rs.next()) {
                    if (username.equals("admin")) {
                        out.println("<h3>" + rs.getString("subject") + "</h3><br/>");
                        out.println("DATE: " + rs.getString("date") + "<br/>");
                        out.println("Expéditeur: " + rs.getString("name") + "<br/>");
                        out.println("Email: " + rs.getString("email") + "<br/>");
                        out.println("Message: " + rs.getString("message") + "<br/>");
                        out.println("<form method=\"post\" action=\"reponseMss.jsp\" name=\"\">");
                        out.println("<input type=\"hidden\" name=\"mid\" value=\"" + mid + "\"/>");
                        out.println("<input type=\"submit\" value=\"Répondre à cette demande\"/>");
                        out.println("</form>");
                    } else if (!username.equals("admin")) {
                        out.println("Vous ne pouvez pas de droit à utiliser cette fonction!");
                    }
                }
                String update = "UPDATE service SET status='o' WHERE sid=" + mid;
                int i = stmt.executeUpdate(update);
                if (i == 1) {
                    out.println("<br/><a href=\"listOrder.jsp\"> Retournez à la page de liste des services</a>");
                }
            }

            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ReadMessage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReadMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ReadMessage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReadMessage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
